package cn.ranko.shiro.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.shiro.mapper.SysClassMapper;
import cn.ranko.shiro.mapper.SysRoleMapper;
import cn.ranko.shiro.mapper.SysUserMapper;
import cn.ranko.shiro.mapper.SysUserRoleMapper;
import cn.ranko.shiro.model.SysClass;
import cn.ranko.shiro.model.SysUser;
import cn.ranko.shiro.model.SysUserRole;
import cn.ranko.shiro.realm.ShiroDbRealm;

@Service
public class SysClassService {
	@Autowired
	SysClassMapper sysClassMapper;
	
    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;
    
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
 
	 
    public List<SysClass> getQuerySysClassAll(){
    	return sysClassMapper.querySysClassAll();
    }
    
	 public SysClass getBySysClassId(Integer class_id) {
        return sysClassMapper.selectByPrimaryKey(class_id);
     }
	 
	  public Datagrid loadList(Pagination pagination) {
		  	List<SysClass> rows = new ArrayList<>();
	        Integer total = sysClassMapper.countOfSysClasslist();
	        Session session = SecurityUtils.getSubject().getSession();
	        SysUser user = (SysUser)session.getAttribute(ShiroDbRealm.SESSION_USER_KEY);
	        if("admin".equals(user.getUsername())){
	        	rows = sysClassMapper.querySysClasslistAll(pagination);
	        }else{
        	    Map<String,Integer> map = new HashMap<>();
	   	        map.put("page", pagination.getPage());
	   	        map.put("rows", pagination.getRows());
	   	        map.put("start",pagination.getStart());
	        	map.put("userId",user.getUserId());
	        	rows = sysClassMapper.querySysClasslist(map);
	        }
	        Datagrid datagrid = new Datagrid();
	        datagrid.setTotal(total);
	        datagrid.setRows(rows);
	        return datagrid;
      }
	  
	  
	   public Integer delete(Integer class_id) {
		   SysClass class1 = new SysClass();
		   class1.setClassId(class_id);
		   class1.setDeleted(1);
	        return sysClassMapper.deleteByPrimaryKey(class1);
	    }
	  
	   public Integer updateSysClass(SysClass sysClass, String roleIds) {
		    if(!StringUtils.isEmpty(roleIds)){
		    	sysClass.setRoleId(Integer.parseInt(roleIds));
		    }
	        return sysClassMapper.updateByPrimaryKey(sysClass);
	    }
	   
	public int save(SysClass sysClass, String roleIds) {
		Integer affect = 0;
		if (!StringUtils.isEmpty(roleIds)) {
			sysClass.setRoleId(Integer.parseInt(roleIds));
		}
		if (sysClass.getClassId() == 0) {
			affect += sysClassMapper.insertSelective(sysClass);
		} else {
			// 编辑
			sysClassMapper.updateByPrimaryKeySelective(sysClass);
		}
		if(StringUtils.isEmpty(roleIds)){
			SysUserRole userRole = new SysUserRole();
			userRole.setRoleId(Integer.parseInt(roleIds));
			sysUserRoleMapper.updateByPrimaryKey(userRole);
		}
//	 	sysUserRoleMapper.deleteByUserId(user.getUserId());
 
        //保存权限
         List<SysUserRole> sysUserRoles = new ArrayList<>();
        String[] splits = roleIds.split(",");
         for(int i=0;i<splits.length;i++){
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(Integer.parseInt(splits[i]));
            sysUserRole.setClassId(sysClass.getClassId());
            affect += sysUserRoleMapper.insert(sysUserRole);
         }
		return affect;
	}
}


